package com.emc.atmos.test.messaging;

import java.util.List;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

import org.apache.log4j.Logger;

public class AtmosRequestMessage {
	/**
	 * Here's what we get from the client
	* 1  POST /rest/objects HTTP/1.1
	* 2  accept: * / *
	3  x-emc-useracl: john=FULL_CONTROL,mary=READ
	4  date: Wed, 18 Feb 2009 16:03:52 GMT
	5  content-type: application/octet-stream
	6  x-emc-date: Wed, 18 Feb 2009 16:03:52 GMT
	7  x-emc-groupacl: other=NONE
	8  host: 168.159.116.96
	9  content-length: 211
	10 x-emc-uid: 33115732f3b7455d9d2344ddd235f4b9/user1
	11 x-emc-signature: KpT+3Ini1W+CS6YwJEAWYWvIlIs=
		NOTE: signature calculation -> signature = Base64(HMACSHA1(HashString))
		*/
	private String hdrHttpRequestMethod;
	private String hdrUserAcl;
	private String hdrGroupAcl;
	private String hdrDate;
	private String hdrContentType;
	private String hdrEMCDate;
	private String hdrEMCGroupAcl;
	private String hdrHost;
	private String hdrRange;
	private String hdrContentLength;
	private String hdrEMCUid;
	private String hdrEMCSignature;
	private String hdrCanonicalizedResource;
	private String hdrEMCListableMeta;
	private String hdrEMCIncludeMeta;
	private String body;
	
	private static Logger log = Logger.getLogger(AtmosRequestMessage.class);
	
	
	public AtmosRequestMessage() {
		
	}
	
	
	 /**
     * Condenses consecutive spaces into one.
     * @param string
     * @return
     */
    private String normalizeSpace(String str) {
		int length = str.length();
		while(true) {
			str = str.replace( "  ", " " );
			if( str.length() == length ) {
				// unchanged
				break;
			}
			length = str.length();
		}
		
		return str;
		
	}
    
    /*
     * headers: 
     * {x-emc-signature=[5fQBIHXF7Peyg00akj3x+IpGeLw=], date=[Fri, 16 Dec 2011 23:52:14 GMT], x-emc-listable-meta=[testId=1], 
     * content-type=[application/octet-stream], x-emc-uid=[test], user-agent=[Java/1.7.0_01], 
     * host=[localhost:8080], accept=[text/html, image/gif, image/jpeg, *; q=.2, * / *; q=.2], 
     * connection=[keep-alive], content-length=[11]}
     */
	public String getHashString () {
		String hashString = "";
		
		hashString = this.getHdrHttpRequestMethod() + "\n" + 			// HTTP REQUEST
						this.getHdrContentType() + "\n" +				// contentType
						"\n" + 											// range
						this.getHdrDate() + "\n" +						// DATE
						//this.getHdrCanonicalizedResource() + "\n";		// Resource
						"/rest/objects" + "\n";
		
		
		//
		// Now Add EMC Headers
		//
		
		// x-emc-listable-meta
		if (!this.getHdrEMCListableMeta().isEmpty()) {
			hashString = hashString + "x-emc-listable-meta:"+normalizeSpace(this.getHdrEMCListableMeta()) + "\n";
		}
		
		// x-emc-uid
		if (!this.getHdrEMCUid().isEmpty()) {
			hashString = hashString + "x-emc-uid:"+normalizeSpace(this.getHdrEMCUid());
		}
		
		log.debug("hash String: " + hashString);
		
		return hashString;
	}
	
	/*
	 * constructor to parse the headers and populate the object
	 */
	//public AtmosRequestMessage(HttpHeaders headers, 
	//							UriInfo uriInfo, 
	//							String body, 
	//							String httpRequestMethod) {
	public AtmosRequestMessage(HttpHeaders headers, 
									UriInfo uriInfo, 
									String httpRequestMethod) {
	
		log.debug("headers: " + headers.getRequestHeaders());
		
		
		List<String> hosts = headers.getRequestHeader("host");
		List<String> contentTypes = headers.getRequestHeader("content-type");
		List<String> ranges = headers.getRequestHeader("range");
		List<String> dates = headers.getRequestHeader("date");
		List<String> canonicalizedResources = headers.getRequestHeader("resource");
		
		// emc headers
		List<String> emcUsers = headers.getRequestHeader("x-emc-uid");
		List<String> emcSignatures = headers.getRequestHeader("x-emc-signature");
		List<String> emcListableMetas = headers.getRequestHeader("x-emc-listable-meta");
		List<String> emcIncludeMetas = headers.getRequestHeader("x-emc-include-meta");
		List<String> userAcls = headers.getRequestHeader("x-emc-useracl");
		List<String> groupAcls = headers.getRequestHeader("x-emc-groupacl");
	
		
		
		this.hdrHttpRequestMethod = httpRequestMethod;
		this.hdrCanonicalizedResource = uriInfo.getAbsolutePath().getPath();
		
		if (emcSignatures != null) {
			this.hdrEMCSignature = emcSignatures.get(0);
		}
		
		if (hosts != null) {
			this.hdrHost = hosts.get(0);
		}
		
		
		if (ranges != null) {
			this.hdrRange = ranges.get(0);
		}
		
		if (contentTypes != null) {
			this.hdrContentType = contentTypes.get(0);
		}
		
	
		if (dates != null) {
			this.hdrDate = dates.get(0);
		}
		
		if (canonicalizedResources != null) {
			this.hdrCanonicalizedResource = canonicalizedResources.get(0);
		}
		
		
		// emc headers
		if (emcIncludeMetas != null) {
			this.hdrEMCIncludeMeta = emcIncludeMetas.get(0);
		}
		
		if (emcListableMetas != null) {
			this.hdrEMCListableMeta = emcListableMetas.get(0);
		}
		
		if (emcUsers != null) {
			this.hdrEMCUid = emcUsers.get(0);
		}
		
		if (userAcls != null) {
			this.hdrUserAcl = userAcls.get(0);
		}
		
		if (groupAcls != null) {
			this.hdrGroupAcl = groupAcls.get(0);
		}
					
		
		// setup the body for convenience
		//this.body = body;
	}
	
	
	
	/*
	 * getters and setters
	 */
	public String getHdrHttpRequestMethod() {
		return hdrHttpRequestMethod;
	}
	public void setHdrHttpRequestMethod(String hdrHttpRequestMethod) {
		this.hdrHttpRequestMethod = hdrHttpRequestMethod;
	}
	public String getHdrUserAcl() {
		return hdrUserAcl;
	}
	public void setHdrUserAcl(String hdrUserAcl) {
		this.hdrUserAcl = hdrUserAcl;
	}
	public String getHdrDate() {
		return hdrDate;
	}
	public void setHdrDate(String hdrDate) {
		this.hdrDate = hdrDate;
	}
	public String getHdrContentType() {
		return hdrContentType;
	}
	public void setHdrContentType(String hdrContentType) {
		this.hdrContentType = hdrContentType;
	}
	public String getHdrEMCDate() {
		return hdrEMCDate;
	}
	public void setHdrEMCDate(String hdrEMCDate) {
		this.hdrEMCDate = hdrEMCDate;
	}
	public String getHdrEMCGroupAcl() {
		return hdrEMCGroupAcl;
	}
	public void setHdrEMCGroupAcl(String hdrEMCGroupAcl) {
		this.hdrEMCGroupAcl = hdrEMCGroupAcl;
	}
	public String getHdrHost() {
		return hdrHost;
	}
	public void setHdrHost(String hdrHost) {
		this.hdrHost = hdrHost;
	}
	public String getHdrContentLength() {
		return hdrContentLength;
	}
	public void setHdrContentLength(String hdrContentLength) {
		this.hdrContentLength = hdrContentLength;
	}
	public String getHdrEMCUid() {
		return hdrEMCUid;
	}
	public void setHdrEMCUid(String hdrEMCUid) {
		this.hdrEMCUid = hdrEMCUid;
	}
	public String getHdrEMCSignature() {
		return hdrEMCSignature;
	}
	public void setHdrEMCSignature(String hdrEMCSignature) {
		this.hdrEMCSignature = hdrEMCSignature;
	}


	public String getHdrCanonicalizedResource() {
		return hdrCanonicalizedResource;
	}


	public void setHdrCanonicalizedResource(String hdrCanonicalizedResource) {
		this.hdrCanonicalizedResource = hdrCanonicalizedResource;
	}

	public String getHdrGroupAcl() {
		return hdrGroupAcl;
	}


	public void setHdrGroupAcl(String hdrGroupAcl) {
		this.hdrGroupAcl = hdrGroupAcl;
	}


	public String getHdrRange() {
		return hdrRange;
	}


	public void setHdrRange(String hdrRange) {
		this.hdrRange = hdrRange;
	}



	public String getBody() {
		return body;
	}



	public void setBody(String body) {
		this.body = body;
	}


	public String getHdrEMCListableMeta() {
		return hdrEMCListableMeta;
	}


	public void setHdrEMCListableMeta(String hdrEMCListableMeta) {
		this.hdrEMCListableMeta = hdrEMCListableMeta;
	}


	public String getHdrEMCIncludeMeta() {
		return hdrEMCIncludeMeta;
	}


	public void setHdrEMCIncludeMeta(String hdrEMCIncludeMeta) {
		this.hdrEMCIncludeMeta = hdrEMCIncludeMeta;
	}
	
	

}
